package com.gjzhao.fmmall.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.gjzhao.fmmall.dao.ProductCommentsMapper;
import com.gjzhao.fmmall.entity.Product;
import com.gjzhao.fmmall.entity.ProductComments;
import com.gjzhao.fmmall.service.ProductCommentsService;
import com.gjzhao.fmmall.vo.PageHelper;
import com.gjzhao.fmmall.vo.RespResultVO;
import com.gjzhao.fmmall.vo.ResponseResultVO;
import com.gjzhao.fmmall.vo.RespCode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @Description: 商品评价 ServiceImpl
 * @Author: gjzhao
 * @Date Created in 2022-01-02 2:46 下午
 */
@Service
public class ProductCommentsServiceImpl implements ProductCommentsService {

    @Autowired
    private ProductCommentsMapper productCommentsMapper;


    /**
     * 获取商品评价
     *
     * @param productId
     * @return
     */
    @Override
    public ResponseResultVO listProductComments(String productId) {

        if (StrUtil.isBlank(productId)) {
            return new ResponseResultVO(RespCode.FAILED.getCode(), "商品编号不能为空", null);
        }

        List<ProductComments> productComments = productCommentsMapper.selectProductCommentsByProductId(productId);

        if (CollectionUtil.isEmpty(productComments)) {
            return new ResponseResultVO(RespCode.SUCCESS.getCode(), "该商品暂无评价", null);
        } else {
            return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", productComments);
        }
    }

    /**
     * 分页方式获取商品评价
     *
     * @param productId 商品id
     * @param pageNum   当前页
     * @param limit     每页条数
     * @return
     */
    @Override
    public ResponseResultVO listProductCommentsPage(String productId,int commType, int pageNum, int limit) {

        if (StrUtil.isBlank(productId)) {
            return new ResponseResultVO(RespCode.FAILED.getCode(), "商品id不能为空", null);
        } else {

            //获取总数
            Example example = new Example(ProductComments.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("productId", productId);
            //评价类型
            if (StrUtil.isNotBlank(String.valueOf(commType))) {
                criteria.andEqualTo("commType", commType);
            }

            int count = productCommentsMapper.selectCountByExample(example);

            //获取页数:如果不能够按照指定的条数分完，则还需要单独加一页
            int pageCount = count % limit == 0 ? count / limit : count / limit + 1;

            //计算起始条数
            int start = (pageNum - 1) * limit;

            //查询数据
            List<ProductComments> productComments = productCommentsMapper.selectProductCommentsPage(productId,commType, start, limit);

            //封装分页对象
            PageHelper<ProductComments> pageHelper = new PageHelper<>();
            pageHelper.setCount(count);
            pageHelper.setCurrentPageNum(pageNum);
            pageHelper.setLimit(limit);
            pageHelper.setPageCount(pageCount);
            pageHelper.setList(productComments);

            if (CollectionUtil.isEmpty(productComments)) {
                return new ResponseResultVO(RespCode.SUCCESS.getCode(), "该商品暂时还没有评论", null);
            } else {
                return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", pageHelper);
            }
        }
    }

    /**
     * 统计商品评论情况
     *
     * @param productId
     * @return
     */
    @Override
    public ResponseResultVO listProductCommentsCount(String productId) {

        //商品评论总数
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        int commentsCount = productCommentsMapper.selectCountByExample(example);

        //好评数
        Example example1 = new Example(ProductComments.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("productId", productId);
        criteria1.andEqualTo("commType", 1);
        int goodCommentsCount = productCommentsMapper.selectCountByExample(example1);

        //中评数
        Example example2 = new Example(ProductComments.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("productId", productId);
        criteria2.andEqualTo("commType", 0);
        int middleCommentsCount = productCommentsMapper.selectCountByExample(example2);

        //差评数
        Example example3 = new Example(ProductComments.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("productId", productId);
        criteria3.andEqualTo("commType", -1);
        int badCommentsCount = productCommentsMapper.selectCountByExample(example3);

        //好评率
        double percent = Double.parseDouble(goodCommentsCount + "") / Double.parseDouble(commentsCount + "") * 100;
        //substring(索引开始出(包括),索引结束处(不包括))  截取止：.出现的索引位置 + 3
        //这种截取方法会随着整数位的增加，小数位动态增加
        String percentValue = (percent + "").substring(0, (percent + "").lastIndexOf(".") + 3);

        //封装统计结果
        HashMap<String, Object> map = new HashMap();
        map.put("commentsCount", commentsCount);
        map.put("goodCommentsCount", goodCommentsCount);
        map.put("middleCommentsCount", middleCommentsCount);
        map.put("badCommentsCount", badCommentsCount);
        map.put("percent", percentValue);

        return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", map);
    }

    /**
     *  添加商品评价
     * @param productComments
     * @return
     */
    @Override
    public ResponseResultVO insertProductComments(ProductComments productComments) {


        String commId = IdUtil.simpleUUID();
        productComments.setCommId(commId);
        productComments.setUserId(productComments.getUserId());//todo 对用户id进行脱敏操作
        productComments.setSepcName(new Date());

        int insertResult = productCommentsMapper.insert(productComments);

        if(insertResult > 1) {
            return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", null);
        }else{
            return new ResponseResultVO(RespCode.FAILED.getCode(), "fail", null);
        }
    }

}
